package com.vates.reservas.web;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vates.reservas.dominio.Hotel;
import com.vates.reservas.service.HotelService;

/**
 * Handles and retrieves the main requests
 */
@Controller
@RequestMapping(value = "/hotel/busquedaPorNombre")
public class HotelBusquedaPorNombreController {

	protected static Logger logger = Logger.getLogger("controller");

	@Autowired
	private HotelService hotelService;

	/**
	 * Handles and retrieves the AJAX Add page
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String gethotelEditar(Model model) {
		model.addAttribute(new Hotel());
		return "hotel/busquedaPorNombre";
	}

	@RequestMapping(method=RequestMethod.POST)
	public String create(@Valid Hotel hotel, BindingResult result) {
		if (result.hasErrors()) {
			return "hotel/busquedaPorNombre";
		}
		return "redirect:/hotel/busquedaPorNombre/" + hotel.getNombre();
	}

	@RequestMapping(value = "{nombre}", method = RequestMethod.GET)
	public String gethotelModificar(@PathVariable String nombre, Model model) {
		Hotel hotel= hotelService.busquedaPorNombre(nombre);
		model.addAttribute(hotel);
		return "hotel/resultadoBusquedaPorNombre";
	}
}